home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / By the Book / Mac C Primer V2 / 3.2 - cdev / cdev.c < prev    next >
C/C++ Source or Header  |  1991-08-20  |  5KB  |  216 lines

  1. /************************************************************/
  2. /*                                                            */
  3. /*    cdev Code from Chapter Three of                            */
  4. /*                                                            */
  5. /*        *** The Macintosh Programming Primer ***            */
  6. /*                                                            */
  7. /*    Copyright 1990, Dave Mark                                */
  8. /*                                                            */
  9. /*    This program demonstrates specific Mac programming        */
  10. /*    techniques.                                                */
  11. /*                                                            */
  12. /************************************************************/
  13.  
  14. #define DEFAULT_ITEM            1
  15. #define USER_ITEM                2
  16.  
  17. #define RUN_ON_ALL_MACHINES        1L
  18. #define ERROR_STATE                0L
  19. #define WORD_RES_ID                -4048
  20. #define FONT_MENU_ID            -4048
  21.  
  22. #define NORMAL_APP_FONT            applFont
  23.  
  24. short    FindFontNumber();
  25.  
  26. typedef struct
  27. {
  28.     short    curFontNum;
  29. } FontNumInfo, **FontNumH;
  30.  
  31.  
  32. pascal long    main( message, item, numItems, cPanelID, e, cDevValue, cpDialog )
  33. int                message, item, numItems, cPanelID;
  34. EventRecord        *e;
  35. long            cDevValue;
  36. DialogPtr        cpDialog;
  37. {
  38.     int            itemType, fontNumber, choice;
  39.     Handle        itemH, tempHandle;
  40.     Rect        itemRect;
  41.     MenuHandle    fontMenu;
  42.     Str255        tempStr;
  43.     
  44.     if ( message == macDev )
  45.         return( RUN_ON_ALL_MACHINES );
  46.     else if ( message == initDev )
  47.     {
  48.         tempHandle = NewHandle( sizeof( FontNumInfo ) );
  49.         fontNumber = (short)FindFontNumber();
  50.         (**((FontNumH)tempHandle)).curFontNum = fontNumber;
  51.         return( (long)tempHandle );
  52.     }
  53.     
  54.     if ( (cDevValue != cdevUnset) && (cDevValue != ERROR_STATE) )
  55.     {
  56.         switch( message )
  57.         {
  58.             case hitDev:
  59.                 if ( item == DEFAULT_ITEM + numItems )
  60.                 {
  61.                     GetDItem( cpDialog, USER_ITEM + numItems, &itemType, &itemH, &itemRect );
  62.                     fontNumber = NORMAL_APP_FONT;
  63.                     SetAppFont( fontNumber );
  64.                     DrawFontName( fontNumber, &itemRect );
  65.                     (**((FontNumH)cDevValue)).curFontNum = fontNumber;
  66.                     FixResource( fontNumber );
  67.                 }
  68.                 else if ( item == USER_ITEM + numItems )
  69.                 {
  70.                     GetDItem( cpDialog, USER_ITEM + numItems, &itemType, &itemH, &itemRect );
  71.                     fontMenu = GetMenu( FONT_MENU_ID );
  72.                     InsertMenu( fontMenu, -1 );
  73.                     AddResMenu( fontMenu, 'FONT' );
  74.                     itemRect.right += 1;
  75.                     choice = DoPopup( &itemRect, fontMenu );
  76.                     
  77.                     if ( choice != 0 )
  78.                     {
  79.                         GetItem( fontMenu, choice, &tempStr );
  80.                         GetFNum( tempStr, &fontNumber );
  81.                         SetAppFont( fontNumber );
  82.                         DrawFontName( fontNumber, &itemRect );
  83.                         (**((FontNumH)cDevValue)).curFontNum = fontNumber;
  84.                         FixResource( fontNumber );
  85.                     }
  86.                     
  87.                     DeleteMenu( FONT_MENU_ID );
  88.                     ReleaseResource( fontMenu );
  89.                 }
  90.                 break;
  91.             case closeDev:
  92.                 DisposHandle( (Handle)cDevValue );
  93.                 break;
  94.             case nulDev:
  95.                 break;
  96.             case updateDev:
  97.                 GetDItem( cpDialog, USER_ITEM+numItems, &itemType, &itemH, &itemRect );
  98.                 FrameRect( &itemRect );
  99.                 MoveTo( itemRect.left + 1, itemRect.bottom );
  100.                 LineTo( itemRect.right, itemRect.bottom );
  101.                 LineTo( itemRect.right, itemRect.top + 1 );
  102.                 fontNumber = (**((FontNumH)cDevValue)).curFontNum;
  103.                 DrawFontName( fontNumber, &itemRect );
  104.                 break;
  105.             case activDev:
  106.                 break;
  107.             case deactivDev:
  108.                 break;
  109.             case keyEvtDev:
  110.                 break;
  111.             case macDev:
  112.                 return( 1L );
  113.                 break;
  114.             case undoDev:
  115.                 break;
  116.             case cutDev:
  117.                 break;
  118.             case copyDev:
  119.                 break;
  120.             case pasteDev:
  121.                 break;
  122.             case clearDev:
  123.                 break;
  124.         }
  125.     }
  126.     
  127.     return( cDevValue );
  128. }
  129.  
  130.  
  131. /********************************    FixResource    *******/
  132.  
  133. FixResource( fontNumber )
  134. short    fontNumber;
  135. {
  136.     Handle    wHandle;
  137.     
  138.     if ( ( wHandle = GetResource( 'word', WORD_RES_ID ) ) != 0L )
  139.       {
  140.           *( (short *)(*wHandle) ) = fontNumber;
  141.           ChangedResource( wHandle );
  142.           WriteResource( wHandle );
  143.       }
  144. }
  145.  
  146.  
  147. /********************************    DoPopup    *******/
  148.  
  149. int        DoPopup( popupRectPtr, theMenu )
  150. Rect        *popupRectPtr;
  151. MenuHandle    theMenu;
  152. {
  153.     Point    popupUpperLeft;
  154.     long    theChoice = 0x0000;
  155.     
  156.     popupUpperLeft.h = popupRectPtr->left + 2;
  157.     popupUpperLeft.v = popupRectPtr->bottom;
  158.     
  159.     LocalToGlobal( &popupUpperLeft );
  160.  
  161.     InvertRect( popupRectPtr );
  162.     theChoice = PopUpMenuSelect( theMenu, popupUpperLeft.v, popupUpperLeft.h, 0 );
  163.     InvertRect( popupRectPtr );
  164.     return( LoWord( theChoice ) );
  165. }
  166.  
  167.  
  168. /**********************************************  FindFontNumber  */
  169.  
  170. short FindFontNumber()
  171. {
  172.     Handle    wHandle;
  173.     short    fontNumber;
  174.     
  175.     if ( ( wHandle = GetResource( 'word', WORD_RES_ID ) ) != 0L )
  176.       {
  177.           fontNumber = *( (short *)(*wHandle) );
  178.           return( fontNumber );
  179.       }
  180.       else
  181.           return( NORMAL_APP_FONT );
  182. }
  183.  
  184.  
  185. /**********************************************  SetAppFont  */
  186.  
  187. SetAppFont( fontNum )
  188. short    fontNum;
  189. {
  190.       *( (short *) 0x0204 ) = fontNum - 1;
  191.       
  192.       WriteParam();
  193. }
  194.  
  195.  
  196. /**********************************************  DrawFontName  */
  197.  
  198. DrawFontName( fontNum, rPtr )
  199. short    fontNum;
  200. Rect    *rPtr;
  201. {
  202.     Str255        tempStr;
  203.     int            w;
  204.     Rect        tempRect;
  205.     
  206.     tempRect = *rPtr;
  207.     InsetRect( &tempRect, 2, 2 );
  208.     EraseRect( &tempRect );
  209.     if ( fontNum == 1 )
  210.         GetFontName( geneva, &tempStr );
  211.     else
  212.         GetFontName( fontNum, &tempStr );
  213.     w = rPtr->right - rPtr->left - StringWidth( tempStr );
  214.     MoveTo( rPtr->left + w/2, rPtr->bottom - 4 );
  215.     DrawString( tempStr );
  216. }